import axios from "axios";
//获取不同环境的参数
import EnvConfig from '@/config'
console.log(EnvConfig);

import userStore from '../store/user'
const user = userStore()

// create an axios instance
const service = axios.create({
    baseURL: EnvConfig.baseURL, // url = base url + request url
    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 5000, // request timeout
});

// 请求拦截器 request interceptor
service.interceptors.request.use(
    config => {
    // do something before request is sent
        config.headers["tenant-id"] = "1"
        if (user.info.accessToken){
            config.headers.Authorization = `Bearer ${user.info.accessToken}`
        }
        // if (store.getters.token) {
            // let each request carry token
            // ['X-Token'] is a custom headers key
            // please modify it according to the actual situation
            //config.headers["X-Token"] = getToken();
        // }
        return config;
    },
    error => {
    // do something with request error
        console.log(error); // for debug
        return Promise.reject(error);
    },
);

// 响应拦截器 response interceptor
// service.interceptors.response.use(
//     /**
//    * If you want to get http information such as headers or status
//    * Please return  response => response
//   */

//     /**
//    * Determine the request status by custom code
//    * Here is just an example
//    * You can also judge the status by HTTP Status Code
//    */
//     response => {
//         const res = response.data;

//         // if the custom code is not 20000, it is judged as an error.
//         if (res.code !== 20000) {
//             alert("12");

//             // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
//             if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
//                 // to re-login
//                 alert("34");
//             }
//             return Promise.reject(new Error(res.message || "Error"));
//         } else {
//             return res;
//         }
//     },
//     error => {
//         console.log("err" + error); // for debug
//         alert("56");
//         return Promise.reject(error);
//     },
// );

class eFlyNetWork{
    //统一post请求接口
    post(url, parameter){
        return new Promise((resolve, reject) => {
            service.post(url, parameter)
                .then(res => {
                    resolve(res);
                })
                .catch(err => {
                    reject(err)
                })
        })
    }
    //统一get请求接口
    get(url, parameter){
        return new Promise((resolve, reject) => {
            service.get(url, parameter)
                .then(res => {
                    resolve(res.data);
                })
                .catch(err => {
                    reject(err)
                })
        })
    }
    //统一上传文件接口
    uploadFile(){

    }
}
//export default service;
export default new eFlyNetWork();
